<script>
import { delete_log } from "@/common/api/play.js"; // 播放
import config from "@/config";  // 配置文件
	export default {
		onLaunch: function() {


			// 首页底部tab 中间按钮事件监听
			uni.onTabBarMidButtonTap((res) => {
				// console.log('触发了1111')
			  uni.navigateTo({
			  	url: "/pages/distinguish/galaxy",
			  });
			});


			// 清除后端播放器记录
			delete_log({
			}).then((res) => {
				// console.log("删除播放", res);
			});

		//   console.log('App Launch')
			this.getSysVersion();
			
			// 在APP.vue里,在uniapp生命周期的onLaunch这个方法里写个延时器
			setTimeout(()=>{
				plus.navigator.closeSplashscreen()
			},5000)
			// uni.navigateTo({
			// 	url:"/pages/login/index"
			// })


			
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
			uni.setStorageSync('isPlay', false);
		},
		methods: {
			// 获取版本 版本更新
			getSysVersion() {
				const _this = this;
				let platform = uni.getSystemInfoSync().platform;
				
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					var oldversion = wgtinfo.version;
					uni.request({
						url: config + '/api/appversion/version', //列表接口地址
						// url: 'https://hx.yitian369.net/api/appversion/version', //列表接口地址
						method: 'get',
						sslVerify: false,
						
						success(res) {
							// console.log('ressrrrrr',res,platform)
							// console.log('oldversion',oldversion)
							if (platform == 'android') {
								if (res.data.status === 200) {	
									var appversion = res.data.data.list[0].version
									var appUrl = res.data.data.list[0].app_url;
									var apptitle = res.data.data.list[0].version;
									var appcontent = res.data.data.list[0].describe;
									// console.log(appUrl, 'appUrlappUrl')
									// console.log(_this.wgtinfoVersionCode.version)
									// 后端返回的app版本和当前app版本比较
									// console.log('appversion',appversion);
									if (oldversion != appversion) {
										uni.showModal({
											title: apptitle,
											content: appcontent,
											success: (res) => {
												console.log(res)
												if (res.confirm ==
													true) { //当用户确定更新，执行更新,下面方法进行下载app
													_this.appdownLoad(appUrl);
												} else {
													// return;
												}
											}
										})
									} else {

									}
								}
							}
						},
						fail(err) {
							console.log('errr',err);
						}
					})
				})
			},
			appdownLoad(url) {
				console.log(url, '下载app地址')

				var that = this;
				uni.showLoading({
					title: '安装包下载中……'
				})
				uni.downloadFile({
					url: url, //文件的下载路径
					success(res) {
						//保存到本地
						uni.saveFile({
							tempFilePath: res.tempFilePath, //文件的临时路径
							success: function(res) {
								console.log(res, '成功成功成功')
								const savedFilePath = res.savedFilePath;
								// 打开文件
								uni.openDocument({
									filePath: savedFilePath,
									success: function(res) {
										uni.hideLoading()
									},
									fail: function(res) {},
									complete: function(res) {
										setTimeout(uni.hideLoading(), 4000)
									},
								});
							},
							fail: function(err) {}
						});
					},
					fail(res) {}
				})
			}
		}
	}
</script>
<style lang="scss">
	@import "@/uni_modules/uview-ui/index.scss";
</style>
<style lang="scss">
	/*每个页面公共css */
	@import './static/css/style.scss';
</style>